import KeywordModel from '../../models/keyword.js';
const keywordModel = new KeywordModel();
Component({
    /**
    * 组件的属性列表
    */
    properties: {
        more: {
            type: String,
            observer: 'loadMore'  //属性改变时触发
        }
    },

    /**
    * 组件的初始数据
    */
    data: {
        historyKeywords: [],
        hotKeywords: [],
        booksList: [],
        booksTotal: 0,
        showSearchResult: false,
        q: '',
        loading: false
    },
    attached(){
        const historyKeywords = keywordModel.getHistory();
        this.setData({
            historyKeywords
        });
        this.getHotSearch();
    },
    /**
    * 组件的方法列表
    */
    methods: {
        getHotSearch(){
            keywordModel.getHot().then((res) => {
                this.setData({
                    hotKeywords: res.hot
                });
            });
        },
        onCancel(){
            this.triggerEvent('cancel', {}, {});
        },
        search(e){
            const word = e.detail.value || e.detail.text;
            this.setData({
                showSearchResult: true,
                booksList: [],
                q: word
            });
            wx.showLoading({
                title: '加载中',
            });
            keywordModel.searchBook(0, 1, word).then((res) => {
                if(res.books.length > 1){
                    keywordModel.addToHistory(word);
                    this.setData({
                        booksList: res.books,
                        booksTotal: res.total
                    });
                }else{
                    wx.showToast({
                        title: '暂无结果',
                        icon: 'none'
                    })
                    // this.onDelete();
                }
                wx.hideLoading();
            });
        },
        onDelete(){
            this.setData({
                showSearchResult: false,
                q: ''
            });
        },
        //点击历史搜索
        onHistorySearch(e){
            this.search(e);
        },
        //上拉加载
        loadMore(){
            if(!this.data.q) return;
            if(this.data.loading) return;
            const len = this.data.booksList.length;
            if (len >= this.data.booksTotal){
                wx.showToast({
                    title: '到底了',
                    icon: 'none'
                });
                return;
            }
            this.data.loading = true;
            wx.showLoading({
                title: '玩命加载中'
            });
            keywordModel.searchBook(len, 1, this.data.q).then((res) => {
                const tempArr = this.data.booksList.concat(res.books);
                this.setData({
                    booksList: tempArr,
                    loading: false   //解锁
                });
                wx.hideLoading();
            }).catch((error) => {
                this.setData({
                    loading: false
                });
            });
        }
    }
})
